/* 8911106 302933981 Ofri Keidar */

/******************************************************************************
* Student Name: Ofri Keidar
* Exercise Name: Ex3
* File Description: Class Node's implementation. The class represents a node in
* 				     a graph. Node object can be printed using a printing
* 				     operator
******************************************************************************/
#include "Node.h"

/***************************************************************************************
* function name: Node(const int number)
* The Input: Number that the new node will hold
* The output: None
* The Function operation: Assigns given number to new node's number attribute
***************************************************************************************/
Node::Node(const int number) {
	m_number = number; // new node's number is given number
}

/***************************************************************************************
* function name: Node(const Node& nodeToCopy)
* The Input: Node to be copied
* The output: None
* The Function operation: Assigns given node's number to new node's number attribute
***************************************************************************************/
Node::Node(const Node& nodeToCopy) {
	m_number = nodeToCopy.m_number; // new node's number is given node's number
}

/***************************************************************************************
* function name: ~Node()
* The Input: None
* The output: None
* The Function operation: Destroys Node object
***************************************************************************************/
Node::~Node() { }

/***************************************************************************************
 * function name: == (const Node& other) const
 * The Input: Node operand to compare with
 * The output: True if given operand is equal to object
 * The Function operation: Compares node's number attributes number and return comparison's
 * 						   result
 ***************************************************************************************/
bool Node::operator == (const Node& other) const {
	return m_number == other.m_number;
}

/***************************************************************************************
 * function name: != (const Node& other) const
 * The Input: Node operand to compare with
 * The output: True if given operand is not equal to object
 * The Function operation: Compares node's number attribute. Operator does not
 * 						   change the object
 ***************************************************************************************/
bool Node::operator != (const Node& other) const {
	return m_number != other.m_number;
}

/***************************************************************************************
 * function name: < (const Node& other) const
 * The Input: Node operand to compare with
 * The output: True if given operand is bigger than object, otherwise False
 * The Function operation: Compares node's number attributes number and return comparison's
 * 						   result
 ***************************************************************************************/
bool Node::operator < (const Node& other) const {
	return m_number < other.m_number;
}

/***************************************************************************************
 * function name: > (const Node& other) const
 * The Input: Node operand to compare with
 * The output: True if object is bigger than given operand, otherwise False
 * The Function operation: Compares node's number attributes number and return comparison's
 * 						   result
 ***************************************************************************************/
bool Node::operator > (const Node& other) const {
	return m_number > other.m_number;
}

/***************************************************************************************
 * function name: toString() const
 * The Input: None
 * The output: String with node's int value
 * The Function operation: Extract node's number attribute's each digit and convert it
 * 						   to a string using ASCII value. Add to output string each digit's
 * 						   string conversion and return this string
 ***************************************************************************************/
 const string Node::toString() const {

	 int nodeNumber; // used to backup node's number and all manipulations will be done on this variable
	 // convert node's number's absolute value
	 if (m_number > 0) {
		 nodeNumber = m_number;
	 } else {
		 nodeNumber = -m_number;
	 }

	 // get node number's right most digit and chop it, stop when no digits are left
	 string resultString = ""; // initialize an empty string to store conversion's result
	 string intrmdtVal = ""; // used to store intermediate value in string concatenation
	 while (nodeNumber != 0) {
		 intrmdtVal = nodeNumber%10 + '0';
		 resultString = intrmdtVal + resultString;
		 nodeNumber /= 10;
	 }

	 // if node number is negative, add "-" at result string's beginning
	 if (m_number < 0) {
		 resultString = "-" + resultString;
	 }

	 return resultString;
 }

/***************************************************************************************
 * function name: operator ++ ()
 * The Input: None
 * The output: The graph node object after its value were incremented
 * The Function operation: Increments node's number attribute
 ***************************************************************************************/
Node& Node::operator++() {
	++m_number; // increment node's number attribute
	return *this; // return node object
}

/***************************************************************************************
 * function name: operator -- ()
 * The Input: None
 * The output: The graph node object after its value were decremented
 * The Function operation: Decrements node's number attribute
 ***************************************************************************************/
Node& Node::operator--() {
	--m_number; // decrement node's number attribute
	return *this; // return node object
}

/***************************************************************************************
* function name: << (ostream &os, const Node& nodeToPrint)
* The Input: Reference to Node to be printed
* The output: ostream reference
* The Function operation: Prints node in format "[NODE_VALUE]"
***************************************************************************************/
ostream& operator << (ostream &os, const Node& nodeToPrint) {
	return os << "[" << nodeToPrint.m_number << "]";
}
